//
//  ByInformationVC.m
//  jmsfhz_ios
//
//  Created by pro on 16/7/18.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByInformationVC.h"

/** 视图 */
#import "BySegmentView.h" 

/** 子控制器 */
#import "ByInfomationChildSuperVC.h"    // 子视图控制器的父类
#import "ByIndexVC.h"                   // 首页
#import "ByOrganizationVC.h"            // 组织机构
#import "ByCooperationVC.h"             // 四方合作
#import "ByRecommendVC.h"               // 人才推荐
#import "ByPracticeVC.h"                // 实践基地
#import "ByAchievementsVC.h"            // 合作成果
#import "ByCompanyVC.h"                 // 企业信息

@interface ByInformationVC () <UIScrollViewDelegate>
{
    NSInteger _curIndex;
}
@property (nonatomic, strong) BySegmentView *segView;

@property (strong, nonatomic) UIScrollView *svContent;

@end

@implementation ByInformationVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"首页";
    
    self.view.backgroundColor = [UIColor whiteColor];
    NSLog(@"重新加载了vc");
    [self createSegmentView];
    
}
- (void)createSegmentView {
    
    self.segView = [[BySegmentView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    __weak typeof(self) weakSelf = self;
    [self.segView setIndexChangeBlock:^(NSInteger index) {
        [weakSelf indexChange:index];
    }];
    
    /** 内容滑动视图 */
    self.svContent = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49 - 40)];
    self.svContent.pagingEnabled = YES;
    self.svContent.showsHorizontalScrollIndicator = NO;
    self.svContent.delegate = self;
    self.svContent.contentSize = CGSizeMake(SCREEN_WIDTH * 7, 0);
    
    [self.view addSubview:self.svContent];
    [self.view addSubview:self.segView];
    
    [self indexChange:0];

}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger index = scrollView.contentOffset.x / pageWidth;
    if (index == _curIndex) {
        return;
    }
    _curIndex = index;
    [self.segView setIndex:index];
    [self addViewController:index];
}

- (void)indexChange:(NSInteger)index {
    [self.svContent setContentOffset:CGPointMake(SCREEN_WIDTH * index, 0) animated:NO];
    [self addViewController:index];
}

- (void)addViewController:(NSInteger)index {
    
    NSMutableArray *maryIndex = [[NSMutableArray alloc] init];
    for (UIViewController *vc in self.childViewControllers) {
        [maryIndex addObject:[NSNumber numberWithInteger:vc.view.tag]];
    }
    
    ByInfomationChildSuperVC *vc;
    if (![maryIndex containsObject:[NSNumber numberWithInteger:index + 100]]) {
        if (index == 0) {           /** 首页 */
            vc = [[ByIndexVC alloc] init];
        } else if (index == 1) {    /** 组织机构 */
            vc = [[ByOrganizationVC alloc] init];
        } else if (index == 2) {    /** 四方合作 */
            vc = [[ByCooperationVC alloc] init];
        } else if (index == 3) {    /** 人才推荐 */
            vc = [[ByRecommendVC alloc] init];
        } else if (index == 4) {    /** 实践基地 */
            vc = [[ByPracticeVC alloc] init];
        } else if (index == 5) {    /** 合作成果 */
            vc = [[ByAchievementsVC alloc] init];
        } else if (index == 6) {    /** 企业信息 */
            vc = [[ByCompanyVC alloc] init];
        } else {
            vc = [[ByInfomationChildSuperVC alloc] init];
        }
        
        vc.view.frame = CGRectMake(SCREEN_WIDTH * index, 0, self.svContent.width, self.svContent.height);
        [self addChildViewController:vc];
        
        [self.svContent addSubview:vc.view];
        vc.view.tag = index + 100;
        [maryIndex addObject:[NSNumber numberWithInteger:vc.view.tag]];
    }


    vc = (ByInfomationChildSuperVC *)[self.svContent viewWithTag:index + 100].viewController;
    [vc refreshTable];
}




#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

    NSLog(@"%@ memory warning", [self class]);
}


@end
